package com.yida.gwj.expend.outside.handler;

import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yida.common.core.exception.ServiceException;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.enums.GwjBusinessLogResult;
import com.yida.gwj.common.utils.GwjEnumUtils;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusiness;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessLog;
import com.yida.gwj.expend.outside.domain.message.MqBaseResult;
import com.yida.gwj.expend.outside.domain.message.PreAcceptExamineMessage;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessRequest;
import com.yida.gwj.expend.outside.enums.BusinessServerStatusEnum;
import com.yida.gwj.expend.outside.enums.BusinessStatusEnum;
import com.yida.gwj.expend.outside.enums.FlagStatusEnum;
import com.yida.gwj.expend.outside.enums.MaintenanceFundsErrorEnum;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessLogService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessService;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;

/**
 * 维修资金预受理
 *
 * @author liangp
 * @date 2023/04/10
 */
@Component
public abstract class AbstractExpendServicePre {

    private final static Logger log = LoggerFactory.getLogger(AbstractExpendServicePre.class);
    private GwjBusinessFlagType businessFlagType;
    @Autowired
    private RemoteGwjConfigService remoteGwjConfigService;
    @Autowired
    private IGwjMaintenanceFundsBusinessService maintenanceFundsBusinessService;
    @Autowired
    private IGwjMaintenanceFundsBusinessLogService maintenanceFundsBusinessLogService;

    public AbstractExpendServicePre() {}

    public AbstractExpendServicePre(GwjBusinessFlagType businessFlagType, RemoteGwjConfigService remoteGwjConfigService, IGwjMaintenanceFundsBusinessService maintenanceFundsBusinessService, IGwjMaintenanceFundsBusinessLogService maintenanceFundsBusinessLogService) {
        this.businessFlagType = businessFlagType;
        this.remoteGwjConfigService = remoteGwjConfigService;
        this.maintenanceFundsBusinessService = maintenanceFundsBusinessService;
        this.maintenanceFundsBusinessLogService = maintenanceFundsBusinessLogService;
    }

    public void preAcceptExamine(MqBaseResult mqBaseResult) {
        // 修改为远程调用
        // PreAcceptExamineMessage preAcceptExamineMessage = JSONUtil.toBean(mqBaseResult.getJsonMsg(),
        // PreAcceptExamineMessage.class);
        // if (ObjectUtil.isNull(preAcceptExamineMessage)) {
        // log.error("预受理审核JSON数据转换失败");
        // return;
        // }
        // try {
        // preAcceptExamineMessage.checkNull();
        // } catch (Exception e) {
        // log.error("参数校验异常 --> [{}]", e);
        // return;
        // }
        // GwjMaintenanceFundsBusiness maintenanceFundsBusiness =
        // maintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
        // {
        // setServerIdentification(preAcceptExamineMessage.getOutServerIdentification());
        // }
        // });
        // if (ObjectUtil.isNull(maintenanceFundsBusiness)) {
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_EXAMINE) +
        // "GwjMaintenanceFundsBusiness为空");
        // }
        // GwjMaintenanceFundsBusiness business = maintenanceFundsBusinessService.updateGwjMaintenanceFundsBusiness(new
        // GwjMaintenanceFundsBusiness() {
        // {
        // // 20230629 hmh 取消内网推送二次补件状态情况下修改了外网申请人信息 start
        // // setCurrentOperId(preAcceptExamineMessage.getOperId());
        // // setCurrentOperName(preAcceptExamineMessage.getOperName());
        // // end
        // setStatus(BusinessStatusEnum.SAVE.getCode());
        // setBizId(maintenanceFundsBusiness.getBizId());
        // setVoteFlag(FlagStatusEnum.SUC.getCode());
        // setSupplementFlag(FlagStatusEnum.SUC.getCode());
        // setOutServerIdentification(preAcceptExamineMessage.getServerIdentification());
        // if (ObjectUtil.notEqual(BusinessServerStatusEnum.RETREAT_PARTS, maintenanceFundsBusiness.getStatus())) {
        // try {
        // GwjConfStep confStep =
        // remoteGwjConfigService.selectNextGwjConfStepByFlagId(maintenanceFundsBusiness.getBizFlagId(),
        // maintenanceFundsBusiness.getStepId(), SecurityConstants.INNER).getData();
        // setStepId(confStep.getStepId());
        // } catch (Exception e) {
        // log.error("GwjConfStep获取异常 -> [{}]", e);
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_EXAMINE) +
        // "获取GwjConfStep异常");
        // }
        // }
        // }
        // });
        // if (ObjectUtil.isNull(business)) {
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_EXAMINE) +
        // "GwjMaintenanceFundsBusiness更新失败");
        // }
        // int insertFlag = maintenanceFundsBusinessLogService.insertGwjMaintenanceFundsBusinessLog(new
        // GwjMaintenanceFundsBusinessLog() {
        // {
        // setAcceptTime(LocalDateTime.now());
        // setStepId(maintenanceFundsBusiness.getStepId());
        // setBizId(maintenanceFundsBusiness.getBizId());
        // setOperId(preAcceptExamineMessage.getOperId());
        // setOperName(preAcceptExamineMessage.getOperName());
        // setContent(preAcceptExamineMessage.getContent());
        // setResult(GwjBusinessLogResult.PASS.getCode());
        // }
        // });
        // if (ObjectUtil.equal(insertFlag, FlagStatusEnum.FAL.getCode())) {
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) +
        // "GwjMaintenanceFundsBusinessLog新增失败");
        // }
    }

    public void preAcceptRetreat(MqBaseResult mqBaseResult) {
        PreAcceptExamineMessage preAcceptExamineMessage = JSONUtil.toBean(mqBaseResult.getJsonMsg(), PreAcceptExamineMessage.class);
        if (ObjectUtil.isNull(preAcceptExamineMessage)) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) + "附件退件转换失败");
        }
        preAcceptExamineMessage.checkNull();
        GwjMaintenanceFundsBusiness maintenanceFundsBusiness = maintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setServerIdentification(preAcceptExamineMessage.getOutServerIdentification());
            }
        });
        if (ObjectUtil.isNull(maintenanceFundsBusiness)) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) + "GwjMaintenanceFundsBusiness为空");
        }
        // 退件情况下进行步骤回退？20230802 注释保留 处理内网退件后，外网保持在申请步骤状态，后期需要再进行变更
        // 获取上一个步骤ID
        // R<GwjConfStep> r =
        // remoteGwjConfigService.selectPreviousGwjConfStepByFlagId(maintenanceFundsBusiness.getBizFlagId(),
        // maintenanceFundsBusiness.getStepId(), SecurityConstants.INNER);
        // if (R.isError(r)) {
        // throw new ServiceException("上一步骤信息获取失败！回退失败！");
        // }
        // Long stepId = StringUtils.isNull(r.getData()) ? null : r.getData().getStepId();
        // e
        GwjMaintenanceFundsBusiness business = maintenanceFundsBusinessService.updateGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusiness() {
            {
                setCurrentOperId(preAcceptExamineMessage.getOperId());
                setCurrentOperName(preAcceptExamineMessage.getOperName());
                setStatus(BusinessStatusEnum.RETREAT_PARTS.getCode());
                setBizId(maintenanceFundsBusiness.getBizId());
                setServerStatus(BusinessServerStatusEnum.RETREAT_PARTS.getCode());
                setServerIdentification(preAcceptExamineMessage.getOutServerIdentification());
                setOutServerIdentification(preAcceptExamineMessage.getServerIdentification());
                // 20230802 退件情况下进行步骤回退 注释保留
                // if (StringUtils.isNotNull(stepId)) {
                // setStepId(stepId);
                // }
            }
        });
        if (ObjectUtil.isNull(business)) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) + "GwjMaintenanceFundsBusiness更新失败");
        }
        int insertFlag = maintenanceFundsBusinessLogService.insertGwjMaintenanceFundsBusinessLog(new GwjMaintenanceFundsBusinessLog() {
            {
                setAcceptTime(LocalDateTime.now());
                setStepId(business.getStepId());
                setBizId(business.getBizId());
                setOperId(preAcceptExamineMessage.getOperId());
                setOperName(preAcceptExamineMessage.getOperName());
                setContent(preAcceptExamineMessage.getContent());
                setResult(GwjBusinessLogResult.RETREAT_PARTS.getCode());
            }
        });
        if (ObjectUtil.equal(insertFlag, FlagStatusEnum.FAL.getCode())) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) + "GwjMaintenanceFundsBusinessLog新增失败");
        }
    }

    // 步骤回调
    public void resultNotify(String jsonMsg) {
        // // MQ消息转换
        // ResultNotifyMessage resultNotifyMessage = JSONUtil.toBean(mqBaseResult.getJsonMsg(),
        // ResultNotifyMessage.class);
        //
        // // 判断消息内容是否为空
        // if(ObjectUtil.isEmpty(resultNotifyMessage)){
        // log.error("预受理审核JSON数据转换失败");
        // return;
        // }
        // 修改为远程调用
        // ResultNotifyMessage resultNotifyMessage = JSONUtil.toBean(jsonMsg, ResultNotifyMessage.class);
        //
        // // 查询外网业务信息
        // GwjMaintenanceFundsBusiness maintenanceFundsBusiness =
        // maintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
        // {
        // // 设置外网业务标志
        // setOutServerIdentification(resultNotifyMessage.getOutServerIdentification());
        // }
        // });
        // // 判断业务信息是否为空
        // if (ObjectUtil.isNull(maintenanceFundsBusiness)) {
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) +
        // "GwjMaintenanceFundsBusiness为空");
        // }
        // // 更新外网业务信息
        // GwjMaintenanceFundsBusiness business = maintenanceFundsBusinessService.updateGwjMaintenanceFundsBusiness(new
        // GwjMaintenanceFundsBusiness() {
        // {
        // // 设置业务主键
        // setBizId(maintenanceFundsBusiness.getBizId());
        // // 查询下一个步骤
        // try {
        // GwjConfStep confStep =
        // remoteGwjConfigService.selectNextGwjConfStepByFlagId(maintenanceFundsBusiness.getBizFlagId(),
        // maintenanceFundsBusiness.getStepId(), SecurityConstants.INNER).getData();
        // setStepId(confStep.getStepId());
        // } catch (Exception e) {
        // log.error("GwjConfStep获取异常 -> [{}]", e);
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_EXAMINE) +
        // "获取GwjConfStep异常");
        // }
        // }
        //
        // });
        // // 判断业务是否更新成功
        // if (ObjectUtil.isNull(business)) {
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) +
        // "GwjMaintenanceFundsBusiness更新失败");
        // }
        // // 插入日志
        // int insertFlag = maintenanceFundsBusinessLogService.insertGwjMaintenanceFundsBusinessLog(new
        // GwjMaintenanceFundsBusinessLog() {
        // {
        // setAcceptTime(LocalDateTime.now());
        // setStepId(business.getStepId());
        // setBizId(business.getBizId());
        // setOperId(resultNotifyMessage.getOperId());
        // setOperName(resultNotifyMessage.getOperName());
        // setContent(resultNotifyMessage.getContent());
        // setResult(GwjBusinessLogResult.SUBMIT.getCode());
        // }
        // });
        // if (ObjectUtil.equal(insertFlag, FlagStatusEnum.FAL.getCode())) {
        // throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.CONF_STEP_CONFIG_CHECK)
        // + "GwjMaintenanceFundsBusinessLog新增失败");
        // }

    }

}
